import request from './request'

// 地址接口类型定义
export interface Address {
  id?: number
  userId: number
  receiverName: string
  receiverPhone: string
  province: string
  city: string
  district: string
  detailAddress: string
  postalCode?: string
  isDefault: boolean
  addressTag?: string
  createdAt?: string
  updatedAt?: string
}

// 获取用户的所有地址
export const getUserAddresses = (userId: number) => {
  return request({
    url: '/addresses',
    method: 'get',
    params: { userId }
  })
}

// 获取用户的默认地址
export const getDefaultAddress = (userId: number) => {
  return request({
    url: '/addresses/default',
    method: 'get',
    params: { userId }
  })
}

// 根据ID获取地址详情
export const getAddressById = (userId: number, addressId: number) => {
  return request({
    url: `/addresses/${addressId}`,
    method: 'get',
    params: { userId }
  })
}

// 创建新地址
export const createAddress = (userId: number, addressData: Omit<Address, 'id' | 'userId' | 'createdAt' | 'updatedAt'>) => {
  return request({
    url: '/addresses',
    method: 'post',
    params: { userId },
    data: addressData
  })
}

// 更新地址
export const updateAddress = (userId: number, addressId: number, addressData: Partial<Address>) => {
  return request({
    url: `/addresses/${addressId}`,
    method: 'put',
    params: { userId },
    data: addressData
  })
}

// 删除地址
export const deleteAddress = (userId: number, addressId: number) => {
  return request({
    url: `/addresses/${addressId}`,
    method: 'delete',
    params: { userId }
  })
}

// 设置默认地址
export const setDefaultAddress = (userId: number, addressId: number) => {
  return request({
    url: `/addresses/${addressId}/default`,
    method: 'put',
    params: { userId }
  })
} 